package com.globallogic.service;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.globallogic.model.Task;

public class PageProcessor {
	
  private WebDriver driver;
  private String baseUrl = "http://portal-ua.globallogic.com/jira-kbp/secure/Dashboard.jspa";

  public PageProcessor() {
    driver = new FirefoxDriver();
  }

  public void start(String login, String password) {
	  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	  
	  driver.get(baseUrl + "/jira-kbp/secure/Dashboard.jspa");

	  driver.switchTo().frame("gadget-0");	  
	  driver.findElement(By.id("login-form-username")).clear();
	  driver.findElement(By.id("login-form-username")).sendKeys(login);
	  driver.findElement(By.id("login-form-password")).clear();
	  driver.findElement(By.id("login-form-password")).sendKeys(password);
	  driver.findElement(By.id("login")).click();
	  
	  Wait<WebDriver> waiter = new WebDriverWait(driver, 10);
	  waiter.until(ExpectedConditions.presenceOfElementLocated(By.id("create_link")));
  }
  
  public void process(Task task, Logger logger) {	  
	  Wait<WebDriver> waiter = new WebDriverWait(driver, 30);
	  waiter.until(ExpectedConditions.presenceOfElementLocated(By.id("create_link")));
	  
	  driver.get(baseUrl);
	  
	  driver.findElement(By.id("create_link")).click();
      
	  waiter.until(ExpectedConditions.presenceOfElementLocated(By.id("create-issue-submit")));
	  
	  WebElement projectField = driver.findElement(By.id("project-field"));    
	  if (!projectField.getAttribute("value").equals(task.project)) {
		  projectField.click();
		  WebElement projectFieldValue = driver.findElement(By.linkText(task.project));
		  projectFieldValue.click();
		  waiter.until(ExpectedConditions.elementToBeClickable(By.id("issuetype-field")));
	  }
    
	  WebElement issueType = driver.findElement(By.id("issuetype-field"));
	  if (!issueType.getAttribute("value").equals(task.issueType)) {
		  issueType.click();
		  WebElement issueTypeValue = driver.findElement(By.linkText(task.issueType));
		  issueTypeValue.click();
		  waiter.until(ExpectedConditions.elementToBeClickable(By.id("summary")));
	  }

	  WebElement summary = driver.findElement(By.id("summary"));
	  summary.clear();
	  summary.sendKeys(task.summary);
        
	  WebElement reporter = driver.findElement(By.id("reporter"));
	  reporter.clear();
	  reporter.sendKeys(task.reporter);
	  reporter.sendKeys(Keys.RETURN);
	  
	  WebElement description = driver.findElement(By.id("description"));
	  description.clear();
	  description.sendKeys(task.description);
	  
	  WebElement location = driver.findElement(By.id("customfield_10468"));
	  location.sendKeys(task.location);
	  
	  WebElement ccTo = driver.findElement(By.id("customfield_10159"));
	  ccTo.clear();
	  ccTo.sendKeys(task.cc);
	  String selectedItemId = "customfield_10159_i_" + task.cc.toLowerCase();
	  waiter.until(ExpectedConditions.elementToBeClickable(By.id(selectedItemId)));
	  driver.findElement(By.id(selectedItemId)).click();

	  WebElement healthcareProgram = driver.findElement(By.id("customfield_10490"));
	  healthcareProgram.sendKeys(task.healthcareProgram);
	  healthcareProgram.sendKeys(Keys.ENTER);
	  
	  WebElement medicalInsurance = driver.findElement(By.id("customfield_10570"));
	  medicalInsurance.sendKeys(task.medicalInsurance);
	  medicalInsurance.sendKeys(Keys.ENTER);
	  
	  WebElement createButton = driver.findElement(By.id("create-issue-submit"));
	  createButton.submit();
	  
	  WebElement issueId = driver.findElement(By.className("issue-created-key"));
	  if (issueId != null) {
		  String message = String.format("<li>%s: <a href=%s>%s</a></li>",
				  task.reporter, issueId.getAttribute("href"), issueId.getText());
		  logger.info(message);
	  }
  }
  
  

  public void finish() {
    driver.quit();
  }

}
